package com.alks.common.utils.excelUtils;

import com.alibaba.excel.EasyExcel;
import com.alks.common.listener.EasyExcelListener;
import com.alks.common.utils.DateUtils;
import com.alks.entity.data.entity.PcLotHead;
import com.alks.entity.exception.ServiceErrorException;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExcelUtils {

    /**
     * 设置页码
     */
    public static ExcelUtilBuild sheet(Integer sheetNo){
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.setSheetNo(sheetNo);
        return utils;
    }

    /**
     * 跳过行数
     */
    public static ExcelUtilBuild passRow(Integer passRow){
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.setPassRow(passRow);
        return utils;
    }

    /**
     * 是否以插入新列的方式新增
     */
    public static ExcelUtilBuild newRow(Boolean newRow){
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.setNewRow(newRow);
        return utils;
    }

    /**
     * 是否自动行宽
     * @param passRow 批量数据前有多少单体数据（单个数据先生成会干扰表头长度默认值）
     */
    public static ExcelUtilBuild autoWidth(int passRow){
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.setAutoColumnWidth(true);
        utils.setPassRow(passRow);
        return utils;
    }

    public static ExcelUtilBuild autoWidth(){
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.setAutoColumnWidth(true);
        return utils;
    }

    /**
     * 设置导出地址
     */
    public static ExcelUtilBuild templetPath(String defaultTempletPath){
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.setDefaultTempletPath(defaultTempletPath);
        return utils;
    }

    public static void creatByTemplate(HttpServletResponse response, String templetName, String excelName, List<?> entityList, Object... entity) throws IOException {
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.creatByTemplate(response, templetName,excelName, entityList, entity);
    }


    public static void creatByTemplate(HttpServletResponse response, String templetName, String excelName, Object entity) throws IOException {
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.creatByTemplate(response, templetName, excelName, entity);
    }
    public static void creatByTemplateM(HttpServletResponse response, String templetName, String excelName, Object entity,List<List<String>> mergeColDataList,List<Integer> mergeColIndexList) throws IOException {
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.creatByTemplateM(response, templetName, excelName, entity, mergeColDataList,mergeColIndexList);
    }

    public static void creatByTemplateWithHandler(HttpServletResponse response, String templetName, String excelName, Object entity) throws IOException {
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.creatByTemplateWithHandler(response, templetName, excelName, entity);
    }
    public static void creatByTemplate(HttpServletResponse response, String templetName, Object entity) throws IOException {
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.creatByTemplate(response, templetName, entity);
    }

    public static void creatByTemplateLargeData(HttpServletResponse response, String templetName, String excelName, List<?> entityList, Object... entity) throws IOException {
        ExcelUtilBuild utils = new ExcelUtilBuild();
        utils.creatByTemplateLargeData(response, templetName,excelName, entityList, entity);
    }


    public static void  setResponseZF(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    }

    //压缩包响应头处理
    public static void  setResponseZIP(HttpServletResponse response, String fileName) throws IOException {
        // 生成压缩包名称
        int dotIndex = fileName.lastIndexOf('.');
        String prefix = (dotIndex != -1) ? fileName.substring(0, dotIndex) : fileName;
        String zipFileName = prefix + ".zip";
//        // 对文件名进行 URL 编码，确保浏览器能正确解析
//        zipFileName = java.net.URLEncoder.encode(zipFileName, StandardCharsets.UTF_8.toString());
//        // 设置响应头，告诉浏览器返回的是Zip压缩文件
//        response.setContentType("application/zip");
//        response.setHeader("Content-Disposition", "attachment; filename=" + zipFileName);

        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, "UTF-8"));
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    }

    public static void  setResponseZF(HttpServletResponse response){
        String fileName= DateUtils.dateTimeNow()+".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    }


    public static <T> List<T> read(MultipartFile file,Class<T> clazz){
        ExcelUtilBuild utils = new ExcelUtilBuild();
        return utils.read(file, clazz);
    };
}
